﻿<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Thread</title>
<meta name="GENERATOR" content="WinCHM">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

</head>

<body><A 
href="http://www.tutorialspoint.com/python/python_multithreading.htm"><FONT 
face=Courier>http://www.tutorialspoint.com/python/python_multithreading.htm</FONT></A><BR>
<H1 
style="MARGIN-BOTTOM: 0px; WORD-SPACING: 0px; FONT: 18px/1.5 Helvetica, Arial, 'Liberation Sans', FreeSans, sans-serif; TEXT-TRANSFORM: none; COLOR: rgb(0,0,0); TEXT-INDENT: 0px; WHITE-SPACE: normal; LETTER-SPACING: normal; BACKGROUND-COLOR: rgb(255,255,255); TEXT-ALIGN: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">Starting 
a New Thread:</H1>
<P 
style="WORD-SPACING: 0px; FONT: 11px verdana, helvetica, arial, sans-serif; TEXT-TRANSFORM: none; COLOR: rgb(0,0,0); TEXT-INDENT: 0px; WHITE-SPACE: normal; LETTER-SPACING: normal; BACKGROUND-COLOR: rgb(255,255,255); TEXT-ALIGN: justify; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">To 
spawn another thread, you need to call following method available in<SPAN 
class=Apple-converted-space>&nbsp;</SPAN><I>thread</I><SPAN 
class=Apple-converted-space>&nbsp;</SPAN>module:</P>
<TABLE class=src 
style="BORDER-RIGHT: rgb(170,170,170) 1px solid; BORDER-TOP: rgb(170,170,170) 1px solid; PADDING-LEFT: 5px; WORD-SPACING: 0px; FONT: 11px verdana, helvetica, arial, sans-serif; VERTICAL-ALIGN: top; TEXT-TRANSFORM: none; BORDER-LEFT: rgb(170,170,170) 1px solid; WIDTH: 552px; COLOR: rgb(0,0,0); TEXT-INDENT: 0px; BORDER-BOTTOM: rgb(170,170,170) 1px solid; WHITE-SPACE: normal; LETTER-SPACING: normal; BORDER-COLLAPSE: collapse; BACKGROUND-COLOR: rgb(241,241,241); TEXT-ALIGN: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px" 
cellPadding=5>
  
  <TR>
    <TD 
    style="BORDER-RIGHT: rgb(170,170,170) 1px solid; BORDER-TOP: rgb(170,170,170) 1px solid; MARGIN-BOTTOM: 0px; VERTICAL-ALIGN: top; BORDER-LEFT: rgb(170,170,170) 1px solid; BORDER-BOTTOM: rgb(170,170,170) 1px solid; FONT-FAMILY: verdana, helvetica, arial, sans-serif; BORDER-COLLAPSE: collapse; TEXT-ALIGN: left"><PRE style="MARGIN-TOP: 0px; FONT-SIZE: 12px; MARGIN-BOTTOM: 0px; MARGIN-LEFT: 0px; FONT-FAMILY: 'Courier New', monospace">thread.start_new_thread ( function, args[, kwargs] )
</PRE></TD></TR></TABLE>
<P 
style="WORD-SPACING: 0px; FONT: 11px verdana, helvetica, arial, sans-serif; TEXT-TRANSFORM: none; COLOR: rgb(0,0,0); TEXT-INDENT: 0px; WHITE-SPACE: normal; LETTER-SPACING: normal; BACKGROUND-COLOR: rgb(255,255,255); TEXT-ALIGN: justify; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">This 
method call enables a fast and efficient way to create new threads in both Linux 
and Windows.</P>
<P 
style="WORD-SPACING: 0px; FONT: 11px verdana, helvetica, arial, sans-serif; TEXT-TRANSFORM: none; COLOR: rgb(0,0,0); TEXT-INDENT: 0px; WHITE-SPACE: normal; LETTER-SPACING: normal; BACKGROUND-COLOR: rgb(255,255,255); TEXT-ALIGN: justify; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">The 
method call returns immediately and the child thread starts and calls function 
with the passed list of<SPAN 
class=Apple-converted-space>&nbsp;</SPAN><I>agrs</I>. When function returns, the 
thread terminates.</P>
<P 
style="WORD-SPACING: 0px; FONT: 11px verdana, helvetica, arial, sans-serif; TEXT-TRANSFORM: none; COLOR: rgb(0,0,0); TEXT-INDENT: 0px; WHITE-SPACE: normal; LETTER-SPACING: normal; BACKGROUND-COLOR: rgb(255,255,255); TEXT-ALIGN: justify; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">Here<SPAN 
class=Apple-converted-space>&nbsp;</SPAN><I>args</I><SPAN 
class=Apple-converted-space>&nbsp;</SPAN>is a tuple of arguments; use an empty 
tuple to call function without passing any arguments.<SPAN 
class=Apple-converted-space>&nbsp;</SPAN><I>kwargs</I><SPAN 
class=Apple-converted-space>&nbsp;</SPAN>is an optional dictionary of keyword 
arguments.</P>
<H2 
style="MARGIN-TOP: 10px; MARGIN-BOTTOM: 10px; WORD-SPACING: 0px; FONT: bold 16px/1.5 Helvetica, Arial, 'Liberation Sans', FreeSans, sans-serif; TEXT-TRANSFORM: none; COLOR: rgb(0,0,0); TEXT-INDENT: 0px; WHITE-SPACE: normal; LETTER-SPACING: normal; BACKGROUND-COLOR: rgb(255,255,255); TEXT-ALIGN: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">Example:</H2>
<TABLE class=src 
style="BORDER-RIGHT: rgb(170,170,170) 1px solid; BORDER-TOP: rgb(170,170,170) 1px solid; PADDING-LEFT: 5px; WORD-SPACING: 0px; FONT: 11px verdana, helvetica, arial, sans-serif; VERTICAL-ALIGN: top; TEXT-TRANSFORM: none; BORDER-LEFT: rgb(170,170,170) 1px solid; WIDTH: 552px; COLOR: rgb(0,0,0); TEXT-INDENT: 0px; BORDER-BOTTOM: rgb(170,170,170) 1px solid; WHITE-SPACE: normal; LETTER-SPACING: normal; BORDER-COLLAPSE: collapse; BACKGROUND-COLOR: rgb(241,241,241); TEXT-ALIGN: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px" 
cellPadding=5>
  
  <TR>
    <TD 
    style="BORDER-RIGHT: rgb(170,170,170) 1px solid; BORDER-TOP: rgb(170,170,170) 1px solid; MARGIN-BOTTOM: 0px; VERTICAL-ALIGN: top; BORDER-LEFT: rgb(170,170,170) 1px solid; BORDER-BOTTOM: rgb(170,170,170) 1px solid; FONT-FAMILY: verdana, helvetica, arial, sans-serif; BORDER-COLLAPSE: collapse; TEXT-ALIGN: left"><PRE style="MARGIN-TOP: 0px; FONT-SIZE: 12px; MARGIN-BOTTOM: 0px; MARGIN-LEFT: 0px; FONT-FAMILY: 'Courier New', monospace">#!/usr/bin/python

import thread
import time

# Define a function for the thread
def print_time( threadName, delay):
   count = 0
   while count &lt; 5:
      time.sleep(delay)
      count += 1
      print "%s: %s" % ( threadName, time.ctime(time.time()) )

# Create two threads as follows
try:
   thread.start_new_thread( print_time, ("Thread-1", 2, ) )
   thread.start_new_thread( print_time, ("Thread-2", 4, ) )
except:
   print "Error: unable to start thread"

while 1:
   pass
</PRE></TD></TR></TABLE>
<P 
style="WORD-SPACING: 0px; FONT: 11px verdana, helvetica, arial, sans-serif; TEXT-TRANSFORM: none; COLOR: rgb(0,0,0); TEXT-INDENT: 0px; WHITE-SPACE: normal; LETTER-SPACING: normal; BACKGROUND-COLOR: rgb(255,255,255); TEXT-ALIGN: justify; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">This 
would produce following result:</P>
<TABLE class=src 
style="BORDER-RIGHT: rgb(170,170,170) 1px solid; BORDER-TOP: rgb(170,170,170) 1px solid; PADDING-LEFT: 5px; WORD-SPACING: 0px; FONT: 11px verdana, helvetica, arial, sans-serif; VERTICAL-ALIGN: top; TEXT-TRANSFORM: none; BORDER-LEFT: rgb(170,170,170) 1px solid; WIDTH: 552px; COLOR: rgb(0,0,0); TEXT-INDENT: 0px; BORDER-BOTTOM: rgb(170,170,170) 1px solid; WHITE-SPACE: normal; LETTER-SPACING: normal; BORDER-COLLAPSE: collapse; BACKGROUND-COLOR: rgb(241,241,241); TEXT-ALIGN: left; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px" 
cellPadding=5>
  
  <TR>
    <TD 
    style="BORDER-RIGHT: rgb(170,170,170) 1px solid; BORDER-TOP: rgb(170,170,170) 1px solid; MARGIN-BOTTOM: 0px; VERTICAL-ALIGN: top; BORDER-LEFT: rgb(170,170,170) 1px solid; BORDER-BOTTOM: rgb(170,170,170) 1px solid; FONT-FAMILY: verdana, helvetica, arial, sans-serif; BORDER-COLLAPSE: collapse; TEXT-ALIGN: left"><PRE style="MARGIN-TOP: 0px; FONT-SIZE: 12px; MARGIN-BOTTOM: 0px; MARGIN-LEFT: 0px; FONT-FAMILY: 'Courier New', monospace">Thread-1: Thu Jan 22 15:42:17 2009
Thread-1: Thu Jan 22 15:42:19 2009
Thread-2: Thu Jan 22 15:42:19 2009
Thread-1: Thu Jan 22 15:42:21 2009
Thread-2: Thu Jan 22 15:42:23 2009
Thread-1: Thu Jan 22 15:42:23 2009
Thread-1: Thu Jan 22 15:42:25 2009
Thread-2: Thu Jan 22 15:42:27 2009
Thread-2: Thu Jan 22 15:42:31 2009
Thread-2: Thu Jan 22 15:42:35 2009
</PRE></TD></TR></TABLE>
<P 
style="WORD-SPACING: 0px; FONT: 11px verdana, helvetica, arial, sans-serif; TEXT-TRANSFORM: none; COLOR: rgb(0,0,0); TEXT-INDENT: 0px; WHITE-SPACE: normal; LETTER-SPACING: normal; BACKGROUND-COLOR: rgb(255,255,255); TEXT-ALIGN: justify; orphans: 2; widows: 2; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">Although 
it is very effective for low-level threading, but the<SPAN 
class=Apple-converted-space>&nbsp;</SPAN><I>thread</I><SPAN 
class=Apple-converted-space>&nbsp;</SPAN>module is very limited compared to the 
newer threading module.</P></body>
</html>
